package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//分类的业务层实现类
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    //查询分类列表
    @Override
    @Cacheable(value = "category",key = "'findAll'")
    public List<Category> findAll() {
        //调用mapper
        return categoryMapper.findAll();
    }

    //新增分类
    @Override
    @CacheEvict(value = "category",key = "#category.id")
    public void save(Category category) {
        //补全分类信息
        category.setId(IdUtil.getSnowflake(1,1).nextId());
//        category.setCreateTime(DateUtil.date());
//        category.setUpdateTime(DateUtil.date());
//        category.setCreateUser(1L);// TODO: 2022/8/27
//        category.setUpdateUser(1L);// TODO: 2022/8/27
        //调用mapper
        categoryMapper.save(category);
    }

    //修改分类
    @Override
    @CacheEvict(value = "category",allEntries = true)
    public void update(Category category) {
        //补全分类信息
//        category.setUpdateTime(DateUtil.date());
//        category.setUpdateUser(1L);// TODO: 2022/8/27
        //调用mapper
        categoryMapper.updateById(category);
    }

    //删除分类
    @Override
    @CacheEvict(value = "category", allEntries = true)
    public ResultInfo delete(Long id) {
        //在菜品表中根据分类id查询菜品的数量
        int dishCount = dishMapper.countByCategoryId(id);
        //数量大于0则返回错误
        if (dishCount > 0) {
            throw new CustomException("该分类下有菜品正在使用,不能删除!");
        }
        //在套餐表中根据id查询套餐的数量
        int setmealCount = setmealMapper.countByCategoryId(id);
        //数量大于0则返回错误
        if (setmealCount > 0) {
            throw new CustomException("该分类下有套餐正在使用,不能删除!");
        }
        //都为0则删除改分类
        categoryMapper.delete(id);
        //返回成功
        return ResultInfo.success();
    }

    //新增菜品之回显分类
    @Override
    @Cacheable(value = "category",key = "#category.type")
    public List<Category> findList(Integer type) {
        //根据菜品type查询分类表
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getType, type);
        //调用mapper
        List<Category> categoryList = categoryMapper.selectList(qw);
        return categoryList;
    }
}